package threadexecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * 功能:
 * 作者: ldl
 * 时间: 2016-08-04 17:09
 */
public class TaskAll implements Callable<Result> {

    private String name;

    public TaskAll(String name) {
        this.name = name;
    }

    @Override
    public Result call() throws Exception {

        System.out.printf("%s: Staring\n", this.name);

        try {
            long duration = (long) (Math.random() * 10);
            System.out.printf("%s: Waiting %d seconds for results.\n", this.name, duration);
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int value = 0;
        for (int i = 0; i < 5; i++) {
            value += (int) (Math.random() * 100);
        }
        Result result = new Result();
        result.setName(name);
        result.setValue(value);

        System.out.println(name + ": End");
        return result;
    }
}
